import Vue from 'vue';
import Router from 'vue-router';

import Home from '@/pages/home/Home';
import Login from '@/pages/login/Login';
import SelectVillage from '@/pages/selectVillage';
import Register from '@/pages/register/index';
//import store from '@/store';

// 供应商管理
import Supplier from '@/pages/supplier';
import SupplierHome from '@/pages/supplier/supplier-home';
import SupplierAdd from '@/pages/supplier/supplier-add';
import SupplierEdit from '@/pages/supplier/supplier-edit';
import SupplierDetail from '@/pages/supplier/supplier-detail';


// 产品管理
import Product from '@/pages/product';
import ProductHome from '@/pages/product/product-home';
import ProductAdd from '@/pages/product/add';
import SelectProduct from '@/pages/buy/add/select-product';
import AddSuccess from '@/pages/buy/add/add-success';

// 二维码
import Qrcode from '@/pages/qrcode';
import QrcodeHome from '@/pages/qrcode/qr-home';
import QrcodePriview from '@/pages/qrcode/preview';

// 预警模块
import Warning from '@/pages/warning';
import WarningHome from '@/pages/warning/warning-home';

// 可视化模块
import Visualization from '@/pages/visualization';
import VisualizationHome from '@/pages/visualization/visualization-home';

// 交互模块
import Interaction from '@/pages/interaction';
import InteractionHome from '@/pages/interaction/interaction-home';

Vue.use(Router)

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: { requiresAuth: false }
  },
  {
    path: '/register',
    name: 'Register',
    component: Register,
    meta: { requiresAuth: false }
  },
  {
    path: '/',
    name: 'Home',
    component: Home,
    meta: { requiresAuth: true }
  },
  {
    path: '/select-supplier',
    name: 'select-supplier',
    component: SelectVillage,
  },
  {
    path: '/supplier',
    // 当组件有默认自路由的时候用:to="{name: 'farmer'}"这样的命名路由是无法渲染默认子路由的
    // 所以不用设置name，然后改用path的形式才行
    // name: 'farmer',
    component: () => import('@/pages/supplier/index'),
    children: [
      {
        path: '',
        name: 'SupplierHome',
        component: () => import('@/pages/supplier/supplier-home')
      },
      {
        path: 'detail',
        name: 'SupplierDetail',
        component: () => import('@/pages/supplier/supplier-detail'),
        meta: { requiresAuth: true }
      },
      {
        path: 'edit',
        name: 'SupplierEdit',
        component: () => import('@/pages/supplier/supplier-edit'),
        meta: { requiresAuth: true }
      },
      {
        path: 'add',
        name: 'SupplierAdd',
        component: () => import('@/pages/supplier/supplier-add'),
        meta: { requiresAuth: true }
      },
      {
        path: 'import-export',
        name: 'ImportExport',
        component: () => import('@/pages/supplier/import-export')
      }
    ]
  },
  {
    path: '/product',
    component: Product,
    children: [
        { path: '', component: ProductHome },
        {
            path: 'add',
            component: ProductAdd,
            children: [
                // { path: '', component: SelectFarmer },
                { path: 'select-product', component: SelectProduct },
                { path: 'add-success', component: AddSuccess }
            ]
        },
        {
            path: 'edit',
            name: 'ProductEdit',
            component: () => import('@/pages/product/product-edit'),
            meta: { requiresAuth: true }
        },
        {
            path: 'detail',
            name: 'ProductDetail',
            component: () => import('@/pages/product/product-detail'), // Assuming product-detail.vue exists or will be created
            meta: { requiresAuth: true }
        }
    ]
  },
  {
    path: '/qrcode',
    component: Qrcode,
    children: [
        { path: '', component: QrcodeHome },
        { path: 'preview', component: QrcodePriview },
    ]
  },
  {
    path: '/warning',
    component: Warning,
    children: [
        { path: '', component: WarningHome },
    ]
  },
  {
    path: '/visualization',
    component: Visualization,
    children: [
        { path: '', component: VisualizationHome },
    ]
  },
  {
    path: '/interaction',
    component: () => import('@/pages/interaction/index'),
    redirect: '/interaction/messages',
    children: [
      {
        path: 'messages',
        name: 'InteractionHome',
        component: () => import('@/pages/interaction/interaction-home'),
        meta: { title: '消息交互' }
      },
      {
        path: 'notifications',
        name: 'Notifications',
        component: () => import('@/pages/interaction/notifications'),
        meta: { title: '通知中心' }
      }
    ]
  },
  {
    path: '/trace',
    component: () => import('@/pages/trace/index'),
    children: [
      {
        path: '',
        name: 'TraceHome',
        component: () => import('@/pages/trace/batch')
      },
      {
        path: 'warehouse',
        name: 'Warehouse',
        component: () => import('@/pages/trace/warehouse')
      },
      {
        path: 'logistics',
        name: 'Logistics',
        component: () => import('@/pages/trace/logistics')
      }
    ]
  },
  {
    path: '/blockchain',
    component: () => import('@/pages/blockchain/index'),
    children: [
      { path: '', component: () => import('@/pages/blockchain/record') },
      { path: 'verify', component: () => import('@/pages/blockchain/verify') }
    ]
  },
  {
    path: '*',
    redirect: '/login'
  },
]

const router = new Router({
	routes,
	mode: 'hash',
//	scrollBehavior (to, from, savedPosition) {
//  if (savedPosition) {
//    return savedPosition
//  } else {
//    return {x: 0, y: 0}
//  }
//}
});

// 路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  if (to.meta.requiresAuth && !token) {
    next('/login')
  } else {
    next()
  }
})

export default router;